package com.lc.redis.redisTemplate;

import com.google.gson.Gson;
import com.lc.redis.entity.User;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@SpringBootTest
public class HashType {

    @Resource
    private RedisTemplate redisTemplate;

    @Test
    public void testSave(){
        User user = new User();
        user.setName("ll");
        user.setSex("男");
        user.setAge(18);
//        HashMap<String, HashMap<String, Object>> stringHashMapHashMap = new HashMap<>();
//        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
//        stringObjectHashMap.put("user",user);
//        stringHashMapHashMap.put("hello1",stringObjectHashMap);
        redisTemplate.opsForHash().put("hello","user",user);
        redisTemplate.expire("hello",1, TimeUnit.MINUTES);
        Object o = redisTemplate.opsForHash().get("hello", "user");
        Gson gson = new Gson();
        String json = gson.toJson(o);
        User user1 = gson.fromJson(json, User.class);
        System.out.println(user1.getSex());
    }

    @Test
    public void testSave2(){
        User user = new User();
        user.setName("ll");
        user.setSex("男");
        user.setAge(18);
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("name","xw");
        stringObjectHashMap.put("age",18);
        stringObjectHashMap.put("sex","男");
        redisTemplate.opsForHash().putAll("user:2",stringObjectHashMap);
        Object o = redisTemplate.opsForHash().entries("user:2");
        System.out.println(o);
    }


}
